package com.ruiysoft.service.mdse.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.entity.integralmdse.MerchandiseSkuMPRela;
import com.ruiysoft.entity.mdse.MerchandisePictureSku;
import com.ruiysoft.entity.mdse.MerchandiseRuleValue;
import com.ruiysoft.entity.mdse.MerchandiseRuleValueRela;
import com.ruiysoft.entity.mdse.MerchandiseSku;
import com.ruiysoft.mapper.integralmdse.MerchandiseSkuMPRelaMapper;
import com.ruiysoft.mapper.mdse.MerchandisePictureSkuMapper;
import com.ruiysoft.mapper.mdse.MerchandiseRuleValueMapper;
import com.ruiysoft.mapper.mdse.MerchandiseRuleValueRelaMapper;
import com.ruiysoft.mapper.mdse.MerchandiseSkuMapper;
import com.ruiysoft.service.mdse.IMerchandiseSkuService;
import com.ruiysoft.service.mdse.bean.SkuDtail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 记录商品的库存和价格 服务实现类
 * </p>
 *
 * @author liyahui
 * @since 2018-10-27
 */
@Service
public class MerchandiseSkuServiceImpl extends ServiceImpl<MerchandiseSkuMapper, MerchandiseSku> implements IMerchandiseSkuService {
    @Autowired
    MerchandisePictureSkuMapper merchandisePictureSkuMapper;

    @Autowired
    MerchandiseRuleValueMapper merchandiseRuleValueMapper;

    @Autowired
    MerchandiseRuleValueRelaMapper merchandiseRuleValueRelaMapper;

    @Autowired
    MerchandiseSkuMPRelaMapper merchandiseSkuMPRelaMapper;


    @Override
    public List<SkuDtail> querySkuList(Long shopId, Long spuId) {
        List<SkuDtail> list = this.getBaseMapper().querySkuList(shopId, spuId);
        for (SkuDtail skuDtail: list
             ) {
            QueryWrapper qw = new QueryWrapper<>().eq("shopid", shopId).eq("sku_id", skuDtail.getSkuId());
            List list2 = merchandisePictureSkuMapper.selectList(qw);
            skuDtail.setListMerchandisePictureSku(list2);

            MerchandiseRuleValueRela merchandiseRuleValueRela = merchandiseRuleValueRelaMapper.selectOne(new QueryWrapper<MerchandiseRuleValueRela>().eq("shopid", shopId).eq("sku_id", skuDtail.getSkuId()));
            if(merchandiseRuleValueRela != null) {
                MerchandiseRuleValue merchandiseRuleValue = merchandiseRuleValueMapper.selectById(merchandiseRuleValueRela.getValueId());
                skuDtail.setMerchandiseRuleValue(merchandiseRuleValue);
                skuDtail.setRuleName(merchandiseRuleValue.getRuleName());
                skuDtail.setRuleValue(merchandiseRuleValue.getRuleValue());
            }

            // 会员价、积分、会员等级
            List<MerchandiseSkuMPRela> list3 = merchandiseSkuMPRelaMapper.selectList(new QueryWrapper<MerchandiseSkuMPRela>().eq("shop_id", shopId).eq("sku_id", skuDtail.getSkuId()));
            skuDtail.setListMerchandiseSkuMPRela(list3);
        }

        return list;
    }

    @Override
    public Integer createOrEditSku(SkuDtail skuDtail) {
        if(this.saveOrUpdate(skuDtail)) {
            int rtn = 0;
            // 规格
            if(skuDtail.getMerchandiseRuleValue() != null && skuDtail.getMerchandiseRuleValue().getId() != null)
                rtn = merchandiseRuleValueMapper.updateById(skuDtail.getMerchandiseRuleValue());
            else {
                MerchandiseRuleValue merchandiseRuleValue = skuDtail.getMerchandiseRuleValue();
                merchandiseRuleValue.setShopid(skuDtail.getShopId());
                merchandiseRuleValue.setSpuId(skuDtail.getSpuId());
                merchandiseRuleValue.setId(skuDtail.getSkuId());
                merchandiseRuleValue.setIsUse(0);

                rtn = merchandiseRuleValueMapper.insert(merchandiseRuleValue);

                // 新建的时候关联规则和sku
                MerchandiseRuleValueRela merchandiseRuleValueRela = new MerchandiseRuleValueRela();
                merchandiseRuleValueRela.setShopid(skuDtail.getShopId());
                merchandiseRuleValueRela.setSkuId(skuDtail.getSkuId());
                merchandiseRuleValueRela.setValueId(merchandiseRuleValue.getId());

                rtn = merchandiseRuleValueRelaMapper.insert(merchandiseRuleValueRela);

            }

            // 轮播图
            // 通过 shopid，skuid 查询所有图片，然后都删了
            List<MerchandisePictureSku> list = merchandisePictureSkuMapper.selectList(new QueryWrapper<MerchandisePictureSku>().eq("shopid", skuDtail.getShopId()).eq("sku_id", skuDtail.getSkuId()));
            List idList = new ArrayList();
            for (MerchandisePictureSku merchandisePictureSku: list
                    ) {
                idList.add(merchandisePictureSku.getId());
            }

            if(idList.size() > 0)
                rtn = merchandisePictureSkuMapper.deleteBatchIds(idList);

            for (MerchandisePictureSku merchandisePictureSku: skuDtail.getListMerchandisePictureSku()
                    ) {
                merchandisePictureSku.setShopid(skuDtail.getShopId());
                merchandisePictureSku.setSkuId(skuDtail.getSkuId());
                merchandisePictureSkuMapper.insert(merchandisePictureSku);
            }

            if(rtn >= 0)
                return 1;
        }

        return -1;
    }
}
